﻿namespace Sogeti.QueryWrapper
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.Server;
    using Microsoft.TeamFoundation.WorkItemTracking.Client;

    public enum StructureType
    {
        Iteration = 1,
        Area = 0
    }

    public class CommonStructureWrapper
    {
        protected ICommonStructureService css;
        protected TfsTeamProjectCollection tpCollection;

        public CommonStructureWrapper(string tpCollectionUrl)
        {
            tpCollection = new TfsTeamProjectCollection(new Uri(tpCollectionUrl));
            tpCollection.EnsureAuthenticated();

            css = (ICommonStructureService)tpCollection.GetService(typeof(ICommonStructureService));
        }

        public CommonStructureWrapper(TfsTeamProjectCollection tp)
        {
            tpCollection = tp;
            css = (ICommonStructureService)tpCollection.GetService(typeof(ICommonStructureService));
        }

        public ProjectInfo GetProjectInfo(string projectUri)
        {
            return css.GetProject(projectUri);
        }

        public string GetProjectName(string projectUri)
        {
            return GetProjectInfo(projectUri).Name;
        }

        public NodeInfo GetNode(string path)
        {
            return css.GetNodeFromPath(path);
        }

        public NodeInfo GetNode(Uri uri)
        {
            return css.GetNode(uri.ToString());
        }

        public string GetNodePath(Uri uri)
        {
            return css.GetNode(uri.ToString()).Path;
        }

        public StructureType GetNodeType(Uri uri)
        {
            if (css.GetNode(uri.ToString()).Path.IndexOf(StructureType.Iteration.ToString()) > 0)
            {
                return StructureType.Iteration;
            }
            else
            {
                return StructureType.Area;
            }
        }

        public XmlNode CreateNode(string nodeName, Uri parentUri, int indexOfParent)
        {
            css.CreateNode(nodeName, parentUri.ToString());
            css.ClearProjectInfoCache();

            XmlElement xmltree;
            xmltree = css.GetNodesXml(new string[] { parentUri.ToString() }, true);

            XmlNode myNode = xmltree.SelectSingleNode(@"//Node[@Name = '" + nodeName + "']");

            if (indexOfParent > 0)
            {
                int moveBy = 1 + indexOfParent - myNode.ParentNode.ChildNodes.Count;

                css.ReorderNode(myNode.Attributes["NodeID"].Value.ToString(), moveBy);
                css.ClearProjectInfoCache();
            }

            return myNode;
        }

        public XmlElement GetNodesTree(StructureType t, string teamProjectName)
        {
            ProjectInfo projectInfo = css.GetProjectFromName(teamProjectName);

            NodeInfo[] nodes = css.ListStructures(projectInfo.Uri);

            XmlElement Xmltree;

            if (t == StructureType.Iteration)
            {
                Xmltree = css.GetNodesXml(new string[] { nodes[1].Uri }, true);
            }
            else
            {
                Xmltree = css.GetNodesXml(new string[] { nodes[0].Uri }, true);
            }

            return Xmltree;
        }

        public bool AddNode(StructureType t, string path, string teamProjectName)
        {
            string[] l = path.Split('\\');

            string nodeName = l.Last<string>();

            string parentPath = path.Substring(0, path.Length - nodeName.Length - 1);
            NodeInfo parentNode = css.GetNodeFromPath(parentPath);

            CreateNode(nodeName, new Uri(parentNode.Uri), -1);

            return true;
        }
    }
}
